package com.yuanfeng.biz.front.user.dao;

import com.yuanfeng.entity.Users;
import com.yuanfeng.util.BaseDao;
import com.yuanfeng.util.JdbcUtil;
import com.yuanfeng.util.MD5Util;

import java.sql.SQLException;
import java.util.UUID;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

public class UserDaoImpl extends BaseDao<Users> implements UserDao {

	public Users findByName(String name) {
		try {
			QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
			return (Users)qr.query("select * from users where name=?",
					new BeanHandler(Users.class),
					new Object[]{name});
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	//保存用户
	public Users save(Users user) {
		try {
			String sql = "insert into users(id,name,password,phone,email) values(?,?,?,?,?)";
			user.setId(UUID.randomUUID().toString().replace("-", ""));
			user.setPassword(MD5Util.md5(user.getPassword().trim()));
			//声明Runner
			QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
			qr.update(sql,new Object[]{user.getId(),user.getName().trim(),user.getPassword().trim(),user.getPhone(),user.getEmail()});
			return user;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

}
